package com.softer.chengxin.project.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.project.dao.ProprietorDao;
import com.softer.chengxin.project.domin.Proprietor;
import com.softer.chengxin.project.service.ProprietorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/16
 */
@Service
public class ProprietorServiceImpl implements ProprietorService {
    @Autowired
    private ProprietorDao proprietorDao;

    /**
     * 增加业主委员会成员
     *
     * @param proprietor 成员实体
     * @return 结果
     */
    @Override
    public CommonResult add(Proprietor proprietor) {
        int num = proprietorDao.add(proprietor);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "新增成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "新增失败");
    }

    /**
     * 查询业主委员会成员列表
     *
     * @param page       页面实体
     * @param proprietor 查询实体
     * @return 列表
     */
    @Override
    public ManagerPageResult queryList(Page page, Proprietor proprietor) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int num = proprietorDao.queryNum(proprietor);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<Proprietor> list = proprietorDao.queryList(page, proprietor);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 查看业主委员会成员详情
     *
     * @param id 成员ID
     * @return 详情
     */
    @Override
    public Proprietor detail(Integer id) {
        return proprietorDao.detail(id);
    }

    /**
     * 删除业主委员会成员
     *
     * @param id 成员ID
     * @return 结果
     */
    @Override
    public CommonResult delete(Integer id) {
        int num = proprietorDao.delete(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败");
    }

    /**
     * 编辑业主委员会成员
     *
     * @param proprietor 成员实体
     * @return 结果
     */
    @Override
    public CommonResult edit(Proprietor proprietor) {
        int num = proprietorDao.edit(proprietor);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败");
    }

    /**
     * 设置返回参数
     *
     * @param mpr  ManagerPageResult
     * @param jr   JsonResult
     * @param num  数量
     * @param list 集合列表
     */
    private ManagerPageResult setResult(ManagerPageResult mpr, JsonResult jr, int num, List list) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }
}
